package com.bw.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bw.domain.SysDictType;
import com.bw.service.SysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 王钰--WY
 * @create 2023-08-15 14:35
 */
@RestController
@RequestMapping("/dict/type")
public class SysDictTypeController {
    @Autowired
    SysDictTypeService sysDictTypeService;

    /**
     * 根据字典名称、字典列表、字典状态、创建时间区间  查询列表
     * @param page
     * @param dictType
     * @return
     */
    @PostMapping("/list")
    public Map<String,Page<SysDictType>> list(Page page, @RequestBody SysDictType dictType){
        Page<SysDictType> pageList=sysDictTypeService.getList(page,dictType);
        Map<String,Page<SysDictType>> map=new HashMap<>();
        map.put("data",pageList);
        return map;
    }
    @GetMapping("/typeList")
    public List<SysDictType> list(){
        List<SysDictType> list = sysDictTypeService.list();
        return list;
    }
    /**
     * 根据字典编号删除
     * @param id
     * @return
     */
    @GetMapping("/delById/{id}")
    public Map<String,Object> delById(@PathVariable("id") Integer id){
        boolean b = sysDictTypeService.removeById(id);
        Map<String,Object> map=new HashMap<>();
        map.put("flag",b);
        return map;
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @GetMapping("/delByIds/{ids}")
    public Map<String,Object> delByIds(@PathVariable("ids") String ids){
        List<String> list = Arrays.asList(ids.split(","));
        boolean b = sysDictTypeService.removeByIds(list);
        Map<String,Object> map=new HashMap<>();
        map.put("flag",b);
        return map;
    }

    /**
     * 新增字典类型
     * @param dictType
     * @return
     */
    @PostMapping("/saveDictType")
    public Map<String,Object> saveDictType(@RequestBody SysDictType dictType){
        boolean b=sysDictTypeService.saveDictType(dictType);
        Map<String,Object> map=new HashMap<>();
        map.put("flag",b);
        return map;
    }

    /**
     * 修改字典类型
     * @param dictType
     * @return
     */
    @PostMapping("/updateDictType")
    public Map<String,Object> updateDictType(@RequestBody SysDictType dictType){
        boolean b=sysDictTypeService.updateDictType(dictType);
        Map<String,Object> map=new HashMap<>();
        map.put("flag",b);
        return map;
    }

    /**
     * 根据id获取字典类型
     * @param id
     * @return
     */
    @GetMapping("/getById/{id}")
    public Map<String,Object> getById(@PathVariable("id")Integer id){
        SysDictType sysDictType = sysDictTypeService.getById(id);
        Map<String,Object> map=new HashMap<>();
        map.put("data",sysDictType);
        return map;
    }
}
